﻿namespace ManagerClinic.Controls
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Modules;
    using System.Collections;
    using System.Configuration;

    public partial class ucChiDinhKhoaPhong : DevExpress.XtraEditors.XtraUserControl
    {
        DmKhoaBusiness dmKhoaBusiness = null;
        DmPhongKhamBusiness dmPhongKhamBusiness = null;

        DmDichVuChiTietBusiness dmDichVuChiTietBusiness = null;
        DmDichVuChiTietEntity objdmDichVuChiTietEntity = null;

        ThuPhiDichVuBusiness thuPhiDichVuBusiness = null;
        DmBacSyBusiness dmBacSyBusiness = null;

        GridCheckMarksSelection gridCheckMarksSelection = null;

        string sMaKhoaKham = ConfigurationManager.AppSettings["MaKhoaKham"].ToString();
        string sMaPhongKham = ConfigurationManager.AppSettings["MaPhongKham"].ToString();
        string sMaKhoaCanLamSang = ConfigurationManager.AppSettings["MaKhoaCanLamSang"].ToString();
        string sMaPhongCanLamSang = ConfigurationManager.AppSettings["MaPhongCanLamSang"].ToString();

        public ucChiDinhKhoaPhong()
        {
            InitializeComponent();
            dmKhoaBusiness = new DmKhoaBusiness();
            dmPhongKhamBusiness = new DmPhongKhamBusiness();
            thuPhiDichVuBusiness = new ThuPhiDichVuBusiness();
            dmDichVuChiTietBusiness = new DmDichVuChiTietBusiness();
            dmBacSyBusiness = new DmBacSyBusiness();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDichVu);
        }

        /// <summary>
        /// Set datasource of lkDmKhoaKham
        /// </summary>
        public void LoadDmKhoaKham()
        {
            var lstDmKhoaEntity = dmKhoaBusiness.GetDmKhoaList();
            lstDmKhoaEntity.Insert(0, new DmKhoaEntity() { MaKhoa = ConstantManagerClinic.sValueMember, TenKhoa = ConstantManagerClinic.sDisplayMember });
            lkKhoaKhamBenh.Properties.DataSource = lstDmKhoaEntity;
            lkKhoaKhamBenh.Properties.DisplayMember = "TenKhoa";
            lkKhoaKhamBenh.Properties.ValueMember = "MaKhoa";
            lkKhoaKhamBenh.EditValue = sMaKhoaKham;
        }

        /// <summary>
        /// Set datasource of lkKhoaCanLamSang
        /// </summary>
        public void LoadDmKhoaCanLamSang()
        {
            var lstDmKhoaEntity = dmKhoaBusiness.GetDmKhoaList();
            lstDmKhoaEntity.Insert(0, new DmKhoaEntity() { MaKhoa = ConstantManagerClinic.sValueMember, TenKhoa = ConstantManagerClinic.sDisplayMember });
            lkKhoaCls.Properties.DataSource = lstDmKhoaEntity;
            lkKhoaCls.Properties.DisplayMember = "TenKhoa";
            lkKhoaCls.Properties.ValueMember = "MaKhoa";
            lkKhoaCls.EditValue = sMaKhoaCanLamSang;
        }

        /// <summary>
        /// Set datasource of lkPhongKham
        /// </summary>
        public void LoadDmPhongKham()
        {
            var lstDmPhongKhamEntity = dmPhongKhamBusiness.GetDmPhongKhamList();
            //set datasource lkPhongKham
            lstDmPhongKhamEntity = lstDmPhongKhamEntity.Where(n => n.MaKhoa == lkKhoaKhamBenh.EditValue.ToString()).ToList();
            lstDmPhongKhamEntity.Insert(0, new DmPhongKhamEntity() { MaPhongKham = ConstantManagerClinic.sValueMember, TenPhongKham = ConstantManagerClinic.sDisplayMember });
            lkPhongKham.Properties.DataSource = lstDmPhongKhamEntity;
            lkPhongKham.Properties.DisplayMember = "TenPhongKham";
            lkPhongKham.Properties.ValueMember = "MaPhongKham";
            lkPhongKham.EditValue = sMaPhongKham;
        }

        /// <summary>
        /// Set datasource of lkPhongCanLamSang
        /// </summary>
        public void LoadDmPhongCanLamSang()
        {
            var lstDmPhongKhamEntity = dmPhongKhamBusiness.GetDmPhongKhamList();
            lstDmPhongKhamEntity = lstDmPhongKhamEntity.Where(n => n.MaKhoa == lkKhoaCls.EditValue.ToString()).ToList();
            lstDmPhongKhamEntity.Insert(0, new DmPhongKhamEntity() { MaPhongKham = ConstantManagerClinic.sValueMember, TenPhongKham = ConstantManagerClinic.sDisplayMember });
            //set datasource lkPhongCls
            lkPhongCls.Properties.DataSource = lstDmPhongKhamEntity;
            lkPhongCls.Properties.DisplayMember = "TenPhongKham";
            lkPhongCls.Properties.ValueMember = "MaPhongKham";
            lkPhongCls.EditValue = sMaPhongCanLamSang;
        }

        /// <summary>
        /// Initial value of lkKhoaKhamBenh
        /// </summary>
        /// <param name="maKhoaKham">MaKhoaKham</param>
        public void setMaKhoaKham(string maKhoaKham)
        {
            lkKhoaKhamBenh.EditValue = maKhoaKham;
        }

        /// <summary>
        /// Initial value of lkKhoaCanLamSang
        /// </summary>
        /// <param name="maKhoaCanLamSang"></param>
        public void setMaKhoaCanLamSang(string maKhoaCanLamSang)
        {
            lkKhoaCls.EditValue = maKhoaCanLamSang;
        }

        /// <summary>
        /// Initial value of lkPhongKhamBenh
        /// </summary>
        /// <param name="maPhongKham"></param>
        public void setMaPhongKhamBenh(string maPhongKham)
        {
            lkPhongKham.EditValue = maPhongKham;
        }

        /// <summary>
        /// Initial value of lkPhongCanLamSang
        /// </summary>
        /// <param name="maPhongCanLamSang"></param>
        public void setMaPhongCanLamSang(string maPhongCanLamSang)
        {
            lkPhongCls.EditValue = maPhongCanLamSang;
        }

        /// <summary>
        /// Get value of lkKhoaKham
        /// </summary>
        /// <returns>MaKhoaKham</returns>
        public string getMaKhoaKham()
        {
            return lkKhoaKhamBenh.EditValue.ToString();
        }

        /// <summary>
        /// Get value of lkPhongKham
        /// </summary>
        /// <returns>MaPhongKham</returns>
        public string getMaPhongKham()
        {
            return lkPhongKham.EditValue.ToString();
        }

        /// <summary>
        /// Get value of lkKhoaCanLamSang
        /// </summary>
        /// <returns>MaKhoaCanLamSang</returns>
        public string getMaKhoaCanLamSang()
        {
            return lkKhoaCls.EditValue.ToString();
        }

        /// <summary>
        /// Get value of lkPhongCanLamSang
        /// </summary>
        /// <returns>MaPhongCanLamSang</returns>
        public string getMaPhongCanLamSang()
        {
            return lkPhongCls.EditValue.ToString();
        }

        /// <summary>
        /// Valid LookupEdit
        /// </summary>
        public void IsValidLookupEdit()
        {
            if (Validation.ValidLookUpEdit(lkKhoaKhamBenh, "khoa khám bệnh") == false) return;
            if (Validation.ValidLookUpEdit(lkPhongKham, "phòng khám") == false) return;
            if (Validation.ValidLookUpEdit(lkKhoaCls, "khoa khám cận lâm sàn") == false) return;
            if (Validation.ValidLookUpEdit(lkPhongCls, "phòng khám cận lâm sàn") == false) return;
        }

        public IList<DmDichVuChiTietEntity> lstDmDichVuChiTietEntity { get; set; }

        /// <summary>
        /// Load datagrid DmDichVuChiTiet
        /// </summary>
        /// <param name="maDichVu">MaDichVu</param>
        public void LoadDmDichVuChiTiet(string maDichVu)
        {
            lstDmDichVuChiTietEntity = dmDichVuChiTietBusiness.GetDmDichVuChiTietByDichVu(maDichVu);
            grdDichVu.DataSource = lstDmDichVuChiTietEntity;
        }

        /// <summary>
        /// Get, set value of DichVuChiTietEntity is checked
        /// </summary>
        public IList<DmDichVuChiTietEntity> DmDichVuChiTietEntityIsChecked { get; set; }

        /// <summary>
        /// Isvalid grid DichVuChiTiet 
        /// </summary>
        /// <returns>All DmDichVuChitietEntity has choosed</returns>
        public void IsValidChooseDichVuChiTiet()
        {
            if (gridCheckMarksSelection.selection.Count <= 0)
                if (gvDichVu.RowCount <= 0) return;

            var lstdmDichVuChiTiet = new List<DmDichVuChiTietEntity>();
            var objChecked = gridCheckMarksSelection.selection;

            foreach (DmDichVuChiTietEntity item in objChecked)
            {
                objdmDichVuChiTietEntity = new DmDichVuChiTietEntity();
                objdmDichVuChiTietEntity = item;
                lstdmDichVuChiTiet.Add(objdmDichVuChiTietEntity);
            }
            if (lstdmDichVuChiTiet.Count > 0) DmDichVuChiTietEntityIsChecked = lstdmDichVuChiTiet;
            // DmDichVuChiTietEntityIsChecked = (IList<DmDichVuChiTietEntity>)gridCheckMarksSelection.selection;

            if (DmDichVuChiTietEntityIsChecked == null || DmDichVuChiTietEntityIsChecked.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn dịch vụ khám chữa bệnh!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            DmDichVuChiTietEntityIsChecked = new List<DmDichVuChiTietEntity>();
            foreach (var o in gridCheckMarksSelection.selection)
            {
                DmDichVuChiTietEntityIsChecked.Add((DmDichVuChiTietEntity)o);
            }
        }

        /// <summary>
        /// Set value has choosed on the grid
        /// </summary>
        /// <param name="maPhieuDichVu">maPhieuDichVu: Ma Phieu Sieu Am, Ma Phieu XQuang ...</param>
        /// <param name="maDichVuKhamChuaBenh">maDichVuKhamChuaBenh: Kham Benh, Sieu Am</param>
        public void setDmDichVuChiTietEntityIsChecked(string maPhieuDichVu, string maDichVuKhamChuaBenh)
        {
            var lstThuPhiEntity = thuPhiDichVuBusiness.GetThuPhiDichVuListByMaPhieuDichVu_MaDichVu(maPhieuDichVu, maDichVuKhamChuaBenh);
            foreach (ThuPhiDichVuEntity thuphiEntity in lstThuPhiEntity)
            {
                for (int i = 0; i < gvDichVu.RowCount; i++)
                {
                    var dmDichVuChiTietEntity = (DmDichVuChiTietEntity)gvDichVu.GetRow(i);
                    if (dmDichVuChiTietEntity.MaChiTieu.Trim() == thuphiEntity.MaChiTieu.Trim())
                    {
                        gridCheckMarksSelection.SelectRow(i, true);
                        continue;
                    }
                }
            }
        }

        private void ucChiDinhKhoaPhong_Load(object sender, EventArgs e)
        {

        }

        private void lkKhoaKhamBenh_EditValueChanged(object sender, EventArgs e)
        {
            LoadDmPhongKham();
        }

        private void lkKhoaCls_EditValueChanged(object sender, EventArgs e)
        {
            LoadDmPhongCanLamSang();
        }

        /// <summary>
        /// Get Yeu Cau
        /// </summary>
        /// <returns>YeuCau</returns>
        public string getYeuCau()
        {
            if (gvDichVu.RowCount <= 0)
                return string.Empty;

            DmDichVuChiTietEntityIsChecked = new List<DmDichVuChiTietEntity>();
            foreach (var o in gridCheckMarksSelection.selection)
                DmDichVuChiTietEntityIsChecked.Add((DmDichVuChiTietEntity)o);

            var lstYeuCauEntity = DmDichVuChiTietEntityIsChecked.Select(n => n.TenChiTieu).ToList();
            return string.Join(",", lstYeuCauEntity.ToArray());
        }

        /// <summary>
        /// Get GhiChu
        /// </summary>
        /// <returns>GhiChu</returns>
        public string getGhiChu()
        {
            return txtGhichu.Text;
        }

        /// <summary>
        /// Set GhiChu
        /// </summary>
        /// <param name="ghiChu">ghiChu</param>
        public void setGhiChu(string ghiChu)
        {
            txtGhichu.Text = ghiChu;
        }

        /// <summary>
        /// Set HoTen bac sy
        /// </summary>
        /// <param name="maBacSy">maBacSy</param>
        public void setBacSy(string maBacSy)
        {
            var dmBacSyEntity = dmBacSyBusiness.GetDmBacSy(maBacSy);
            if (dmBacSyEntity == null) return;
            txtBacSykham.Text = dmBacSyEntity.MaBacSy + " - " + dmBacSyEntity.TenBacSy;
        }
    }
}
